//service path manager

package it.unibo.deis.lia.mmhc.spm;

import it.unibo.deis.lia.mmhc.nip.NetworkInterfaceProvider;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import java.net.BindException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Vector;

public class ServicePathServer extends Thread {

	private String address;
	private int port;
	private boolean go = true;
	private Vector<SocketOut> socketOut;

    static Vector<LocalServiceSocket> localservice;
	static Vector<ServiceServer> serviceServer;

	public ServicePathServer(String address, int port) {
		super();
		this.address = address;
		this.port = port;
		this.socketOut = new Vector<SocketOut>();
		this.localservice = new Vector<LocalServiceSocket>();
		this.serviceServer = new Vector<ServiceServer>();
	}

	public void run() {
		try {
			ServerSocket ss = null;
			try {
				ss = new ServerSocket(2500, 0, InetAddress.getByName(address));
				System.err
						.println("\tCreato ServicePathService  sull'indirizzo:"
								+ address);
			} catch (BindException e) {
				go = false;
			}

			while (go) {// go verifica se la connessione è già stata attivata o
				// se si verificano degli errori!

				Socket input = ss.accept();
				// String remoteSenderIP =
				// input.getInetAddress().getHostAddress();
				new ServicePathServerThread(input).start();

			}
		} catch (Exception e) {
			System.err
					.println("Errore nel ServicePathServer relativo all'indirizzo: "
							+ address);
		}
	}

	private void setSok(ServicePacket server, Socket out) {
		for (int i = 0; i < serviceServer.size(); i++) {

			if (serviceServer.elementAt(i).getPort() == server.getPort()
					&& serviceServer.elementAt(i).getFornitore()
							.equalsIgnoreCase(server.getFornitore())
					&& serviceServer.elementAt(i).getRichiedente()
							.equalsIgnoreCase(server.getRichiedente())) {
				serviceServer.elementAt(i).setSoket(out);
			}
		}

	}

	public static Socket getSocket(String name, int port2) {
		for (int i = 0; i < localservice.size(); i++) {
			if (localservice.elementAt(i).getName().equalsIgnoreCase(name)
					&& localservice.elementAt(i).getPorta() == port2) {
				return localservice.elementAt(i).getSock();

			}
		}
		return null;
	}

	private Socket getsocket(Vector<SocketOut> vector, String stringa) {
		for (int i = 0; i < vector.size(); i++) {
			if (vector.elementAt(i).getIp().equalsIgnoreCase(stringa))
				return vector.elementAt(i).getOut();
		}

		SocketOut sockOut = new SocketOut(stringa);
		socketOut.add(sockOut);

		return sockOut.getOut();

	}

	public boolean isGo() {
		return go;
	}

	public void setGo(boolean go) {
		this.go = go;
	}

	private class SocketOut {

		String ip;
		Socket out;
		SocketVector vec=SocketVector.getInstance();

		public SocketOut(String ip) {
			super();
			this.ip = ip;
		
			Socket 	
					out = vec.getSoket(ip, 2500);
				
		}

		public String getIp() {
			return ip;
		}

		public Socket getOut() {
			return out;
		}

	}

	public static void addLocalService(String name, int port, Socket sock) {
		boolean cegia = false;
		for (int i = 0; i < localservice.size(); i++) {
			if (localservice.elementAt(i).getName().equalsIgnoreCase(name)
					&& localservice.elementAt(i).getPorta() == port) {
				cegia = true;
				if (localservice.elementAt(i).getSock().isClosed()) {
					localservice.remove(i);
					cegia = false;
				}
				break;
			}
		}
		if (!cegia)
			localservice.add(new LocalServiceSocket(name, port, sock));
	}

	public static void addServerService(ServiceServer server) {
		boolean cegia = false;
		for (int i = 0; i < serviceServer.size(); i++) {
			if (serviceServer.elementAt(i).getName1().equalsIgnoreCase(
					server.getName1())
					&& serviceServer.elementAt(i).getPort() == server.getPort()
					&& serviceServer.elementAt(i).getFornitore()
							.equalsIgnoreCase(server.getFornitore())
					&& serviceServer.elementAt(i).getRichiedente()
							.equalsIgnoreCase(server.getRichiedente())) {
				cegia = true;
				break;
			}

		}
		if (!cegia) {
			serviceServer.add(server);
			System.err.println("Aggiunto serviceserver");
		}

	}

}
